﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Rescue_of_the_Dwarvenzombies.Rendering.RenderStates
{
    /// <summary>
    /// PauseRenderState provides the draw method thats activated when the RenderManager draws 
    /// during the PauseState.
    /// </summary>
    /// <remarks></remarks>
    internal class PauseRenderState : RenderState
    {
        /// <summary>
        /// 
        /// </summary>
        private RenderState _parentRenderState;
        /// <summary>
        /// 
        /// </summary>
        private string _text;
        /// <summary>
        /// 
        /// </summary>
        private Color _textColor = Color.White;
        /// <summary>
        /// 
        /// </summary>
        private Vector2 _screenCenter;
        /// <summary>
        /// 
        /// </summary>
        private float fader;

        /// <summary>
        /// Initializes a new instance of the <see cref="PauseRenderState"/> class.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <param name="renderStateManager">The render state manager.</param>
        /// <param name="parentRenderState">State of the parent render.</param>
        /// <remarks></remarks>
        public PauseRenderState(Game game, RenderStateManager renderStateManager, RenderState parentRenderState)
            : base(game, renderStateManager)
        {
            _parentRenderState = parentRenderState;
            LoadContent();
            fader = 0f;
        }

        /// <summary>
        /// Loads the content.
        /// </summary>
        /// <remarks></remarks>
        private void LoadContent()
        {
            SpriteFont = Game.Content.Load<SpriteFont>("Fonts/PauseFont");

            _text = "Paused";
            Vector2 textLength = SpriteFont.MeasureString(_text);

            //Find the center of the screen for the text.
            _screenCenter = new Vector2(Game.GraphicsDevice.Viewport.Width * 0.5f - textLength.X * 0.5f, Game.GraphicsDevice.Viewport.Height * 0.5f - textLength.Y * 0.5f);
        }

        /// <summary>
        /// Updates the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <remarks></remarks>
        public override void Update(GameTime gameTime)
        {
            if (fader < 1)
            {
                fader += (float)(gameTime.ElapsedGameTime.TotalSeconds);
            }
        }

        /// <summary>
        /// Draws this instance.
        /// </summary>
        /// <remarks></remarks>
        public override void Draw()
        {

            SpriteBatch.Begin();

            //Draw playingrenderstate
            _parentRenderState.Draw();

            //Draw paused state.
            SpriteBatch.DrawString(SpriteFont, _text, _screenCenter, _textColor * fader);

            SpriteBatch.End();
        }
    }
}